/*
 * @lc app=leetcode.cn id=696 lang=typescript
 *
 * [696] 计数二进制子串
 */

// @lc code=start

// TODO 分解法比较稳妥，其余的暴力解法不太行
function countBinarySubstrings(s: string): number {
    let count = 0;
    let selfCount = 1;
    let selfVal = s[0];
    for (let i = 1; i < s.length; i++) {
        const element = s[i];
        if (element === selfVal) {
            if (check(s, i - selfCount * 2 - 1, i)) count++;
            selfCount++;
        } else {
            selfCount = 1;
            selfVal = element;
            count++;
        }
    }
    return count;
}
const check = (s: string, l: number, r: number) => {
    if (l < 0) return false;
    const head = s[l];
    const tail = s[r];
    l++;
    r--;
    while (l < r) {
        if (s[l] !== head || s[r] !== tail) {
            return false;
        }
        l++;
        r--;
    }
    return true;
};
// @lc code=end
